package uit.v_t.file_transfer;

import java.io.File;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{
	Button btnBack;
	Button btnSwitch;
	
	
	GridView gridItems;
	
	
	ArrayAdapter<String> adapter;

	
	public static final String ACTION_WIDGET = "uit.v_t.file_transfer.MainActivity.ACTION_WIDGET";

	private static final String PREFS_NAME = "ManagerPrefsFile"; // user
																	// preference
																	// file name
	private static final String PREFS_HIDDEN = "hidden";
	private static final String PREFS_COLOR = "color";
	private static final String PREFS_THUMBNAIL = "thumbnail";
	private static final String PREFS_SORT = "sort";
	


	private FileManager mFileMag;
	private EventHandler mHandler;
	private EventHandler.TableRow mTable;

	private SharedPreferences mSettings;
	private TextView mPathLabel, mDetailLabel, mStorageLabel;
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		/* read settings */
		mSettings = getSharedPreferences(PREFS_NAME, 0);
		boolean hide = mSettings.getBoolean(PREFS_HIDDEN, false);
		boolean thumb = mSettings.getBoolean(PREFS_THUMBNAIL, true);		
		int color = mSettings.getInt(PREFS_COLOR, -1);
		int sort = mSettings.getInt(PREFS_SORT, 3);
		
		mFileMag = new FileManager();
		mFileMag.setShowHiddenFiles(hide); // khoi tao ban dau la ko an
		mFileMag.setSortType(sort); // khoi tao ban dau la sap xep tang dan theo
									// dung luong

		if (savedInstanceState != null)
			mHandler = new EventHandler(MainActivity.this, mFileMag,
					savedInstanceState.getString("location"));
		else
			mHandler = new EventHandler(MainActivity.this, mFileMag); // tai day khoi
																// tao duong dan
																// den thu muc
																// hien tai de
																// lay danh sach

		mHandler.setTextColor(color);
		mHandler.setShowThumbnails(thumb); // khoi tao ban dau la tao thumbnail
		mTable = mHandler.new TableRow(); // tao mot danh sach theo duong dan
											// khoi tao ban sau

		
		btnBack = (Button) findViewById(R.id.back_button);
		btnBack.setOnClickListener(mHandler);
		
		btnSwitch = (Button) findViewById(R.id.switch_button);
//		btnSwitch.setOnClickListener(new View.OnClickListener() {
//			
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				if (isListView) {
//					isListView = false;
//					listItems.setVisibility(View.GONE);
//					
//					gridItems.setVisibility(View.VISIBLE);
//					gridItems.setAdapter(listItems.getAdapter());					
//				} else {
//					isListView = true;
//					listItems.setVisibility(View.VISIBLE);
//					listItems.setAdapter(gridItems.getAdapter());
//					gridItems.setVisibility(View.GONE);
//				}
//			}
//		});
		
		/*
		listItems = (ListView) findViewById(R.id.items_list);
		listItems.setOnItemClickListener(listClickedItem);	
		
		if (isListLayout) {
			mHandler.setListAdapter(mTable);		
			listItems.setVisibility(View.VISIBLE);
			listItems.setAdapter(mTable);
		} else {
			
		}
		*/
		gridItems = (GridView) findViewById(R.id.items_grid);
		gridItems.setOnItemClickListener(listClickedItem);
		//gridItems.setOnItemLongClickListener(longClickListener);
		
		mHandler.setListAdapter(mTable);
		gridItems.setAdapter(mTable);
		gridItems.setVisibility(View.VISIBLE);
		
		
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("location", mFileMag.getCurrentDir());
	}
	
	public OnItemClickListener listClickedItem = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			// TODO Auto-generated method stub
			final String item = mHandler.getData(position);
			File file = new File(mFileMag.getCurrentDir() + "/" + item);
			String item_ext = null;
			
			try {
				item_ext = item.substring(item.lastIndexOf("."), item.length());

			} catch (IndexOutOfBoundsException e) {
				item_ext = "";
			}
			
			if (file.isDirectory()) {
				if (file.canRead()) {
					mHandler.stopThumbnailThread();
					// item = sdcard
					mHandler.updateDirectory(mFileMag.getNextDir(item, false));
					//mPathLabel.setText(mFileMag.getCurrentDir());

					/*
					 * set back button switch to true (this will be better
					 * implemented later)
					 */
//					if (!mUseBackKey)
//						mUseBackKey = true;

				} else {
					Toast.makeText(MainActivity.this,
							"Can't read folder due to permissions",
							Toast.LENGTH_SHORT).show();
				}
			}
		}		
	};
	
//	public OnItemLongClickListener longClickListener = new OnItemLongClickListener() {
//
//		public boolean onItemLongClick(AdapterView<?> parent, View v,
//				int position, long id) {
//			// TODO Auto-generated method stub
//			v.setOnTouchListener(MainActivity.this);
//			return true;
//		}
//	};
	
	
}
